package udp;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.*;

/**
 * Date:2022/2/18
 * Author:ybc
 * Description:
 */
public class SendTest {

    public static void main(String[] args) throws IOException {
        //创建发送方的Socket对象
        DatagramSocket sendSocket = new DatagramSocket();
        //创建封装数据的数据报包对象
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String msg = null;
        while ((msg = reader.readLine()) != null){
            byte[] b = msg.getBytes();
            InetAddress inetAddress = InetAddress.getByName("127.0.0.1");
            DatagramPacket packet = new DatagramPacket(b, b.length, inetAddress, 12345);
            //发送数据
            sendSocket.send(packet);
        }
        //关闭资源
        sendSocket.close();
    }

    public static void test() throws IOException {
        //创建发送方的Socket对象
        DatagramSocket sendSocket = new DatagramSocket();
        //创建封装数据的数据报包对象
        byte[] b = "hello,UDP receive".getBytes();
        InetAddress inetAddress = InetAddress.getByName("127.0.0.1");
        DatagramPacket packet = new DatagramPacket(b, b.length, inetAddress, 12345);
        //发送数据
        sendSocket.send(packet);
        //关闭资源
        sendSocket.close();
    }

}